//
//  Student.m
//  SQL演练
//
//  Created by Guo Rui on 2017/10/25.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "Student.h"
#import "SQLTool.h"

@implementation Student
+ (instancetype)name:(NSString *)name age:(NSInteger)age score:(CGFloat)score
{
    Student *student = [[Student alloc] init];
    student.name = name;
    student.age = age;
    student.score = score;
    return student;
}

- (BOOL)insertToTable
{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO t_student (name,age,score) VALUES (\'%@\',%zd,%lf)", self.name, self.age, self.score];
    const char *sql_c = [sql UTF8String];
    return [SQLTool.sharedInstance excute:sql_c errMsg:NULL];
}

- (BOOL)deleteFromTable
{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM t_student WHERE name=\'%@\'", self.name];
    const char *sql_c = [sql UTF8String];
    return [SQLTool.sharedInstance excute:sql_c errMsg:NULL];
}

+ (BOOL)updateWithStudent:(Student *)student
{
    NSString *sql = [NSString stringWithFormat:@"UPDATE t_student SET name=\'%@\', age=%zd, score=%lf WHERE name=\'%@\'", student.name, student.age, student.score, student.name];
    const char *sql_c = [sql UTF8String];
    return [SQLTool.sharedInstance excute:sql_c errMsg:NULL];
}

@end
